const fs = require("fs");

function convertFile(filename) {
  const data = fs.readFileSync(filename, "utf-8");
  return convert(data);
}

function convertFile2(inFile, outFile) {
  const content = convertFile(inFile);
  fs.writeFileSync(outFile, content, { encoding: "utf-8" });
}

function convert(content) {
  class Book {
    constructor(ctx) {
      this.ctx = ctx.split("\n");
    }

    trimPageNumber() {
      this.ctx = this.ctx.map((e) => e.replace(/\s+\d+$/, ""));
      return this;
    }

    addH1() {
      this.ctx = this.ctx.map((e) =>
        e.replace(/^((第\s*\d+\s*章)|(附录)|(后记)|(第.*部分)|(项目))/, "# $1")
      );
      return this;
    }

    addH2() {
      this.ctx = this.ctx.map((e) => e.replace(/^(\d+\.\d+\s+)/, "## $1"));
      return this;
    }

    addH3() {
      this.ctx = this.ctx.map((e) => e.replace(/(\d+\.\d+\.\d+\s+)/, "### $1"));
      return this;
    }
    get content() {
      return this.ctx.join("\n");
    }
  }
  const book = new Book(content);
  book.trimPageNumber().addH1().addH2().addH3();
  return book.content;
}

const source = String.raw`D:\LiuZhaoHui\私人网盘\笔记\Obsidian\工作笔记\Python\Python编程\0.1-目录.md`;
const dest = String.raw`D:\LiuZhaoHui\私人网盘\笔记\Obsidian\工作笔记\Python\Python编程\0.1-目录标题.md`;
convertFile2(source, dest);
